<?php include '_init_.php';?>
<?php include 'page_header.php';?>
<?php
$act = isset($_REQUEST['act'])?$_REQUEST['act']:'';

if($act == 'save')
{
	if(!isset($_REQUEST['id']) || intval($_REQUEST['id']) < 1)
		unset($_REQUEST['id']);
		
	$catalogHelper = new Catalog_Helper();
	if(intval($_REQUEST['id']) > 0)
		$catalogHelper->init($_REQUEST['id']);
	$catalogHelper->setInfo($_REQUEST);
	ob_clean();
	header('Location:backstage.php');
	exit();
}
if($act == 'edit')
{
	$json = new Services_JSON();
	$catalogHelper = new Catalog_Helper();
	$catalogHelper->init($_REQUEST['id']);
	$info = $catalogHelper->getInfo();
	$info['json_photo_size'] = $json->encode($info['photo_size']);
	if($info['json_photo_size'] == 'null')
		$info['json_photo_size'] = '[]';
	$info['json_photo_size_frame'] = $json->encode($info['photo_size_frame']);
	if($info['json_photo_size_frame'] == 'null')
		$info['json_photo_size_frame'] = '{}';
	$info['json_gift'] = $json->encode($info['gift']);
	if($info['json_gift'] == 'null')
		$info['json_gift'] = '[]';
}
if($act == '')
{
	$info = array();
	$info['json_photo_size'] = '[]';
	$info['json_photo_size_frame'] = '{}';
	$info['json_gift'] = '[]';
}
?>
<div id="main"><!-- start #main //-->
<form method="post" action='backstage-edit.php'>
<input type="hidden" name="act" value="save"/>
<input type="hidden" name="id" value="<?php echo $info['id'];?>"/>
<input type="hidden" name="_size_table" value=""/>
<input type="hidden" name="_size_frame_table" value=""/>
<input type="hidden" name="_gift_table" value=""/>
<div id="tita"> <!-- start #tita //-->
<?php define('ADMIN_NAVIGATOR_ITEM','預約包套管理');?>
<?php include 'navigator_admin.php';?>
<?php define('ADMIN_MENU_CURRENT_ITEM','包套建立');?>
<?php include 'admin_menu.php';?>
<script language="javascript">
function edit_toa() {
		rAlert("backstage-editia.php" , null, "相片尺寸列表", 600, 300);
}
function edit_tob(size_id,frame_index,frame_id,size_name) {
		rAlert("backstage-editib.php?size_id="+size_id+"&frame_index="+frame_index+"&frame_id="+frame_id+'&size_name='+size_name , null, "像框管理", 625, 160);
}
function edit_toc() {
		rAlert("backstage-editic.php" , null, "贈品管理", 625, 260);
}
$(function(){
//set info value to ui
$('select[name=type]').val('<?php echo $info['type'];?>');
$('select[name=benzu_style_id]').val('<?php echo $info['benzu_style_id'];?>');
$('select[name=person_number]').val('<?php echo $info['person_number'];?>');
$('select[name=person_number2]').val('<?php echo $info['person_number2'];?>');
$('select[name=dress_number]').val('<?php echo $info['dress_number'];?>');
$('select[name=dress_number2]').val('<?php echo $info['dress_number2'];?>');
});
</script>

<div class="clearfix" id="mainf"><!-- start #mainf //-->
<div class="mainf-3">
<ul>
    <li class="maina-1h">
        預約方案<img src="images/line3.gif" width="21" height="12" /><input name="name" type="text"  class="maine-keyinf" value="<?php echo $info['name'];?>"/></li>
        <li class="maina-1h">
        包套金額<img src="images/line3.gif" width="21" height="12" /><input name="price" type="text"  class="maine-keyinf _validate_form_float" value="<?php echo $info['price'];?>"/></li>
     <li class="mainf-1e">預約類別
       <label for="select2"></label>
       <select name="type" class="maine-keyinm" id="select2" >
         <option value="寶寶照">寶寶照</option>
         <option value="全家福">全家福</option>
         <option value="個人照">個人照</option>
       </select>
     </li>
      <li class="mainf-1e">數位光碟<input name="disk_number" type="text"  class="maine-keying _validate_form_int" value="<?php echo $info['disk_number'];?>"/></li>
      <li class="mainf-1e">相本數量<input name="benzu_number" type="text"  class="maine-keying _validate_form_int" value="<?php echo $info['benzu_number'];?>"/></li>
        <li class="mainf-1e">修改像本增加差價<input name="benzu_customize_price" type="text"  class="maine-keying _validate_form_float" value="<?php echo $info['benzu_customize_price'];?>"/>&nbsp;元</li>
    
        <li class="mainf-1e">相本樣式
       <label for="select2"></label>
       <select name="benzu_style_id" class="maine-keyinm2" id="select2" >
       <?php
    	   $dtBenzuStyle = new DataTable(constant('DB_TABLE_BENZU_STYLE'));
        	$data_list = $dtBenzuStyle->get_list('','name');
        	foreach($data_list as $tmpData)
        	{
        		printf("<option value='%d'>%s</option>",Util::html_safe($tmpData['id']),Util::html_safe($tmpData['name']));
        	}
        ?>
       </select>
     </li>
    </ul>
     </div>
   
   
     <div class="mainf-3">
    <ul>
     <li class="mainf-1e" style="width: 338px;">拍攝人數（成人）
     
       <label for="select"></label>
       <select name="person_number" class="maine-keyinh" id="select">
         <option value="1">1</option>
         <option value="2">2</option>
         <option value="3">3</option>
         <option value="4">4</option>
         <option value="5">5</option>
         <option value="6">6</option>
         <option value="7">7</option>
         <option value="8">8</option>
         <option value="9">9</option>
         <option value="10">10</option>
       </select>
       
     </li>
      <li class="mainf-1e" style="width: 338px;">服裝造型（成人）
      
      <label for="select"></label>
      <select name="dress_number" class="maine-keyinh" id="select">
         <option value="1">1</option>
         <option value="2">2</option>
         <option value="3">3</option>
         <option value="4">4</option>
         <option value="5">5</option>
         <option value="6">6</option>
         <option value="7">7</option>
         <option value="8">8</option>
         <option value="9">9</option>
         <option value="10">10</option>
       </select>
       </li>
       <li class="mainf-1e" style="width: 338px;">拍攝人數（小孩）
     
       <label for="select"></label>
       <select name="person_number2" class="maine-keyinh" id="select">
         <option value="1">1</option>
         <option value="2">2</option>
         <option value="3">3</option>
         <option value="4">4</option>
         <option value="5">5</option>
         <option value="6">6</option>
         <option value="7">7</option>
         <option value="8">8</option>
         <option value="9">9</option>
         <option value="10">10</option>
       </select>
       
     </li>
      <li class="mainf-1e" style="width: 338px;">服裝造型（小孩）
      
      <label for="select"></label>
      <select name="dress_number2" class="maine-keyinh" id="select">
         <option value="1">1</option>
         <option value="2">2</option>
         <option value="3">3</option>
         <option value="4">4</option>
         <option value="5">5</option>
         <option value="6">6</option>
         <option value="7">7</option>
         <option value="8">8</option>
         <option value="9">9</option>
         <option value="10">10</option>
       </select>
       </li>
      </ul>
     </div>
     
     <script language="javascript">
     var global_size_table = <?php echo $info['json_photo_size'];?>;
     var global_size_frame_table = <?php echo $info['json_photo_size_frame'];?>;
     function dialog_function_get_size_info()
     {
     	return global_size_table;
     }
     function dialog_function_set_size_callback(sizeInfo)
     {
     	rClose();
     	global_size_table = sizeInfo;
     	refreshSizeHtml();
     }
     $(function(){
     	refreshSizeHtml();
     	refreshGiftHtml();
     });
     function refreshSizeHtml()
     {
     	//delete li except the last one
     	var liLen = $('#html_sizecontainer li').length - 1 ;
     	$('#html_sizecontainer li').each(function(i,v){
     		if(i < liLen)
     		{
     			$(v).remove();
     		}
     	});
     	$.each(global_size_table,function(k,v){
     		var newItem = $('<li class="maina-1a">放大<img width="8" height="8" src="images/line3.gif">'+v.size_name+' 吋 X '+v.number+'</li>');
     		if($('#html_sizecontainer li').length > 1)
     		{
     			newItem.insertAfter($('#html_sizecontainer li:first'));
     		}else{
     			 newItem.insertBefore($('#html_sizecontainer li:last'));
     		}
     	});
     	//refresh size frame selector
     	//create a new list then copy necessary data to the new array, end assign the new array to data and refresh ui
     	var tmp_table = [];
     	$.each(global_size_table,function(k,v){
     		if(tmp_table[v.size_id] == undefined)
     		{
     			tmp_table[v.size_id] = new Array();
     		}
     		var oldDataSize = 0;
     		if(global_size_frame_table[v.size_id])
     		{
     			oldDataSize = global_size_frame_table[v.size_id].length;
     		}
     		for(i= 0; i < v.number; i++)
     		{
     			if(i < oldDataSize)
     			{
     				tmp_table[v.size_id][i] = global_size_frame_table[v.size_id][i];
     			}else{
     				tmp_table[v.size_id][i] = false;
     			}
     		}
     	});
     	global_size_frame_table = tmp_table;
     	refresh_size_frame_ui();
  
     }
     function refresh_size_frame_ui()
     {
     	//refresh ui
     	$('#html_sizeframe_container li').remove();
     	for(si = 0; si < global_size_table.length;si++)
     	{
     	for(var k in global_size_frame_table)
     	{
     		if(k != global_size_table[si].size_id)
     		{
     			continue;
     		}
     		v = global_size_frame_table[k];
     			$.each(v,function(k2,v2){
     				//get size name
     				var size_name = '';
     				$.each(global_size_table,function(k3,v3){
     					if(v3.size_id == k)
     					{
     						size_name = v3.size_name
     						return;
     					}
     				});
     			  if(v2)
     			  {
     			  	  var newItem = $('<li class="maina-1j">'+size_name+' 像框<img src="images/line3.gif" width="21" height="12" />'+v2.frame_name+'<img src="images/line3.gif" width="21" height="12" /><a href="#" onclick="edit_tob('+k+','+k2+','+v2.frame_id+',\''+size_name+'\'); return false;">選擇</a></li></li>');
     			  	  $('#html_sizeframe_container').append(newItem);
     			  	
     			  }else{
     			  	  var newItem = $('<li class="maina-1j">'+size_name+' 像框<img src="images/line3.gif" width="21" height="12" /><a href="#" onclick="edit_tob('+k+','+k2+',\'\',\''+size_name+'\'); return false;">選擇</a></li>');
     			  	 $('#html_sizeframe_container').append(newItem);
     			  	
     			  }
      		});
      		
      		break;
     	}
     	}
     }
     function dialog_function_set_size_frame_callback(size_id,frame_index,frame_id,frame_name)
     {
     	rClose();
     	global_size_frame_table[size_id][frame_index] = {'frame_id':frame_id,'frame_name':frame_name};
     	refresh_size_frame_ui();
     }
     </script>
     <div class="mainf-3">
    <ul id="html_sizecontainer">
          <li class="maina-1d"><a href="#" onclick="javascript:edit_toa();return false;"><img src="images/edit-add.gif" width="107" height="17" /></a></li>
      </ul>
     </div>
     <div class="mainf-3">
    <ul id="html_sizeframe_container">

      </ul>
     </div>

<script language="javascript">
 var global_gift_table = <?php echo $info['json_gift'];?>;
function dialog_function_get_gift_info()
{
    return global_gift_table;
}
function dialog_function_set_gift_callback(giftInfo)
{
     	rClose();
     	global_gift_table = giftInfo;
     	refreshGiftHtml();
}
function refreshGiftHtml()
{
	//delete li except the last one
     	var liLen = $('#html_gift_container li').length - 1 ;
     	$('#html_gift_container li').each(function(i,v){
     		if(i < liLen)
     		{
     			$(v).remove();
     		}
     	});
     	$.each(global_gift_table,function(k,v){
     		var newItem = $('<li class="maina-1a">'+v.gift_name+'<img src="images/line3.gif" width="21" height="12" />'+v.number+'</li>');
     		if($('#html_gift_container li').length > 1)
     		{
     			newItem.insertAfter($('#html_gift_container li:first'));
     		}else{
     			 newItem.insertBefore($('#html_gift_container li:last'));
     		}
     	});
}
</script>
<div class="mainf-3">
    <ul id="html_gift_container">
     
       
          <li class="maina-1d"><a href="#" onclick="javascript:edit_toc(1118);return false;"><img src="images/edit-add.gif" width="107" height="17" /></a></li>
      </ul>
     </div>
     
</div><!-- end #maine //-->
  
  	<script language="javascript">
  	function saveForm()
  	{
  		$('input[name=_size_table]').val($.toJSON(global_size_table));
  		$('input[name=_size_frame_table]').val($.toJSON(global_size_frame_table));
  		$('input[name=_gift_table]').val($.toJSON(global_gift_table));
  		document.forms[0].submit();
  		return false;
  	}
  	</script>
    <div id="pageh" style="margin-bottom:30px;"><!-- start #pageh //-->
    <div id="pageh-4" ><a href="#" onclick="return saveForm();"><img src="images/backstage-btn-4.gif" width="78" height="30"/></a></div>
    
  </div><!-- end #pageh //-->
  </form>
  
</div><!-- end #main //-->
<?php include 'page_footer.php';?>